package com.robcity.platform;

import com.robcity.platform.application.NettyUdpServer;
import com.robcity.platform.application.Server;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.MutablePropertySources;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
 *
 */

@SpringBootApplication
@EnableAspectJAutoProxy
@MapperScan(basePackages = {"com.robcity.platform.mapper"})
public class RobcityPlatformService extends SpringBootServletInitializer implements CommandLineRunner {
    @Resource
    private ConfigurableEnvironment springEnv;
    @Resource
    Server server;
    @Resource
    NettyUdpServer nettyUdpServer;

    public static void main(String[] args) {
        SpringApplication.run(RobcityPlatformService.class, args);
    }


    @Override
    public void run(String... args) throws Exception {
        server.init();
//        nettyUdpServer.bind(5002);

        List<String> secriteConfig = Arrays.asList("ssilas.aliyun-sms.access-key-secret", "silas.aliyun-sms.access-key", "aliyun.oss.accessKeySecret", "aliyun.oss.accessKeyId",
                "spring.datasource.username", "spring.datasource.password", "spring.redis.username", "spring.redis.password", "spring.mail.username", "spring.mail.password",
                "isz.app-id", "isz.app-key");
        MutablePropertySources propSrcs = springEnv.getPropertySources();
        // 获取所有配置
        Map<String, String> props = StreamSupport.stream(propSrcs.spliterator(), false)
                .filter(ps -> ps instanceof EnumerablePropertySource)
                .map(ps -> ((EnumerablePropertySource) ps).getPropertyNames())
                .flatMap(Arrays::stream)
                .distinct()
                .collect(Collectors.toMap(Function.identity(), springEnv::getProperty));

        // key 和 value 之间的最小间隙
        int interval = 20;
        int max = props.keySet().stream()
                .max(Comparator.comparingInt(String::length))
                .orElse("")
                .length();

        // 打印
        props.keySet().stream()
                .sorted()
                .forEach(k -> {
                    int i = max - k.length() + interval;
                    String join = String.join("", Collections.nCopies(i, " "));
                    if (secriteConfig.contains(k)) {
                        System.out.println(String.format("%s%s%s", k, join, Base64.getEncoder().encodeToString(props.get(k).getBytes())));
                    } else {
                        System.out.println(String.format("%s%s%s", k, join, props.get(k)));
                    }
                });
    }

}

